package communication.majiang
{
	import flash.utils.Dictionary;
	
	import communication.CertManager;
	import communication.ServerAddress;
	
	import modules.GameDispatcher;
	import modules.GameEvent;
	import modules.lobby.LobbyEvent;
	
	import protobuf.HSPKG_SELECT_TABLE_ACK;
	import protobuf.TABLE_BRIEF_INFO;


	public class MSManager
	{
		public static var instance:MSManager;

		public static function init():void
		{
			logger("麻将socket模块初始化");
			if (instance == null)
			{
				instance = new MSManager();
			}
		}

		public function MSManager()
		{
			addListeners();
		}

		/**
		 * 桌子编号与服务器地址对应表
		 **/
		private var tableIdAddressDic:Dictionary = new Dictionary();

		/**
		 *  服务器地址与socket对应表
		 */
		public var addressSocketDic:Dictionary = new Dictionary();

		/**
		 * 桌子编号与socket对应表
		 */
		public var tableIdSocketDic:Dictionary = new Dictionary();

		private function get dispatcher():GameDispatcher
		{
			return GameDispatcher.instance;
		}

		private function get certManager():CertManager
		{
			return CertManager.getInstance();
		}

		/**
		 * 添加侦听器
		 */
		private function addListeners():void
		{
			dispatcher.addEventListener(LobbyEvent.GET_TABLE_LIST, onGetTableList);
			
			dispatcher.addEventListener(LobbyEvent.UPDATE_TOURNAMENT_TABLELIST, onGetTournamentTableList);
			
			dispatcher.addEventListener(LobbyEvent.ADD_TABLEID_ADDRESS, onAddTableIdAddress);
			
			dispatcher.addEventListener(GameEvent.majiang_server_close,onMajiangServerClose);
		}

		/**
		 * 创建麻将socket
		 */
		public function createMajiangSocket():void
		{
//			logger("创建麻将socket，初始化桌子编号、服务器地址、socket的对应关系");

			for (var key:String in tableIdAddressDic)
			{
				var address:ServerAddress = tableIdAddressDic[key];
				if(addressSocketDic[address.toString()] == null)
				{
					addressSocketDic[address.toString()] = new MajiangSocket([address]);
				}
				tableIdSocketDic[key] = addressSocketDic[address.toString()];
			}
			
			initMajiangMsgSender()
		}

		/**
		 * 初始化麻将信息发送者
		 */
		private function initMajiangMsgSender():void
		{
			MajiangMsgSender.init();
		}

		/**
		 * 获取桌子列表数据
		 */
		private function onGetTableList(event:LobbyEvent):void
		{
			var tableDataList:HSPKG_SELECT_TABLE_ACK = event.data;

			var serverAddress:ServerAddress;
			for each (var tableBriefInfo:TABLE_BRIEF_INFO in tableDataList.tables)
			{
				serverAddress = new ServerAddress();
				serverAddress.host = certManager.convertByteArrayToString(tableBriefInfo.ip);
				serverAddress.port = tableBriefInfo.port;
				//这里的policy使用config.xml中配置的值
				serverAddress.policy = GlobalData.policy;

				//保存桌子编号与地址的对应关系
				tableIdAddressDic[tableBriefInfo.tableId.toString()] = serverAddress;
			}

			createMajiangSocket();
		}
		
		/**
		 * 获取锦标赛桌子列表数据
		 */
		private function onGetTournamentTableList(event:LobbyEvent):void
		{
			/*var tableDataList:HSPKG_TNMT_TABLELIST_ACK = event.data;
			
			var tournament:TournamentModel = LobbyDataModel.getTournamentById(tableDataList.tournamentId);

			var serverAddress:ServerAddress;
			for each (var tableBriefInfo:TNMT_TABLE_INFO in tableDataList.tables)
			{
				serverAddress = new ServerAddress();
				serverAddress.host = tournament.ip;
				serverAddress.port = tournament.port;
				//这里的policy使用config.xml中配置的值
				serverAddress.policy = GlobalData.policy;

				//保存桌子编号与地址的对应关系
				tableIdAddressDic[tableBriefInfo.tableid.toString()] = serverAddress;
			}
			
			createMajiangSocket();*/
		}
		
		/**
		 * 如果没有的话，补充一个桌子编号与地址的对应关系
		 */
		private function onAddTableIdAddress(event:LobbyEvent):void
		{
			var tableIdAddress:Object = event.data;
			
			var serverAddress:ServerAddress = tableIdAddressDic[tableIdAddress.tableId.toString()];
			if(serverAddress == null)
			{
				serverAddress = new ServerAddress();

				//保存桌子编号与地址的对应关系
				tableIdAddressDic[tableIdAddress.tableId.toString()] = serverAddress;
			}
			serverAddress.host = tableIdAddress.ip;
			serverAddress.port = tableIdAddress.port;
			//这里的policy使用config.xml中配置的值
			serverAddress.policy = GlobalData.policy;
			
			createMajiangSocket();
		}
		
		private function onMajiangServerClose(event:GameEvent):void
		{
			/*var closeSocket:MajiangSocket = event.data.socket;
			for(var key:String in tableIdSocketDic)
			{
				if(tableIdSocketDic[key] && TablesDataManager.tableDataModeDic[key])
				{
					//离开桌子
					dispatcher.dispatchEvent(new MajiangMsgEvent(MSProto.MSID_LEAVE_TABLE_ACK.toString(),{tableIdStr:key}));
				}
			}*/
		}
	}
}
